<?php

namespace App\Controller\Wap;

use App\Entity\ShopGoods;
use App\Entity\UserLikesProduct;
use App\Model\CommentsModel;
use App\Model\ShopGoodsModel;
use App\Model\UserLikesProductModel;
use App\Base\BaseController4User;
use App\Model\LazyCommon\CCM;
use App\Model\LazyCommon\PlatConfigModel;
use App\Model\LazyCommon\UserModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;


/**
 * @route("/wap")
 */
class ProductController extends BaseController4User
{

    private function initData()
    {
        return $this->data;
    }

    /**
     * @route("/product/index",name="UserAppProductIndex")
     */
    public function UserAppProductIndex()
    {
        $data = $this->initData();
        $uid = $this->isUser() ? $this->isUser() : 0;
        if(!$id = $this->getParamRequired("id")){return $this->json_out(0,"");}

        $ShopGoodsModel = new ShopGoodsModel($this->getDoctrine());
        $shopGoods = $ShopGoodsModel->getRep()->find($id);
        $shopGoods = $this->toArray($shopGoods);
        $shopGoods["params"] = mb_split(",|\，|(\s)",$shopGoods["params"]);
        $data["goods"] = $shopGoods;
        $data["goodsJson"] = json_encode($shopGoods);

        //获取收藏信息
        $UserLikesProductModel = new UserLikesProductModel($this->getDoctrine());
        $data["likes"] = $UserLikesProductModel->getLikeStatus($uid,$id);

        //获取客服二维码
        $PlatConfigModel = new PlatConfigModel($this->getDoctrine());
        $PlatConfig = $PlatConfigModel->get(3);//todo 平台设置ID
        $pic = $PlatConfig->getPic();
        $data["serviceQR"] = $pic;

        //获取我的评论
        $CommentsModel = new CommentsModel($this->getDoctrine());
        $comments = $CommentsModel->getUserProductComment($uid,"ShopGoods",$id);
        if ($comments) {

            $UserModel = new UserModel($this->getDoctrine());
            $user = $UserModel->getUserById($comments->getWho());
            $newData = array(
                "pic" => $user->getHeadPic(),
                "nickName" => $user->getNickName(),
                "text" => $comments->getText(),
                "addTime" => date(YYMMDDHHIISS,$comments->getAddTime()),
            );
        }else{
            $newData = "";
        }

        $data["myComments"] = $newData;



        return $this->render("App/Wap/product_index.php.twig",$data);
    }


    /**
     * @route("/product/likes",name="UserAppProductLikes")
     */
    public function UserAppProductLikes()
    {
        if(!$what = $this->getParamRequired("what")){return $this->json_out(0,"参数缺失");}
        $uid = $this->needToBeUser();
        $UserLikesProductModel = new UserLikesProductModel($this->getDoctrine());
        $UserLikesProduct = $UserLikesProductModel->check($uid,$what);
        if ($UserLikesProduct) {
            //收藏状态，取消收藏
            $UserLikesProductModel->del($uid,$what);
            $msg = "已取消";
        } else {
            //未收藏状态，添加收藏
            $UserLikesProductModel->add($uid,$what);
            $msg = "已收藏";
        }
        return $this->json_out(1,$msg);
    }



    /**
     * @route("/product/comments/add",name="UserAppProductCommentsAdd")
     */
    public function UserAppProductCommentsAdd()
    {
        $uid = $this->needToBeUser();
        if(!$what = $this->getParamRequired("what")){return $this->json_out(0,"参数缺失");}
        if(!$text = $this->getParamRequired("text")){return $this->json_out(0,"请输入评论内容");}
        $CommentsModel = new CommentsModel($this->getDoctrine());
        $comments = $CommentsModel->addProduct($uid,$what,$text);
        return $this->json_out(1,"保存成功",$this->toArray($comments));
    }



    /**
     * @route("/product/comments/get",name="UserAppProductCommentsGet")
     */
    public function UserAppProductCommentsGet()
    {
        $uid = $this->needToBeUser();
        if(!$what = $this->getParamRequired("what")){return $this->json_out(0,"参数缺失");}
        if(!$page = $this->getParamRequired("page")){
            $page = 0;
        }
        $CommentsModel = new CommentsModel($this->getDoctrine());
        $comments = $CommentsModel->getProduct($what,$uid,$page);
        if (!$comments) {
            $comments = "";
        }
        return $this->json_out(1,"数据获取成功",($comments));
    }






}
